package com.luhuiguo.cms.client.ui;

import java.util.List;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.GlobalConstants;
import com.luhuiguo.cms.client.GlobalMessages;
import com.luhuiguo.cms.client.GlobalResources;
import com.luhuiguo.cms.client.presenter.ChannelsPresenter;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.model.ChannelTreeModel;

@Singleton
public class ChannelsView extends ContentPanel implements ChannelsPresenter.Display {

    @SuppressWarnings("unused")
    private GlobalConstants constants;

    private GlobalMessages messages;

    private GlobalResources resources;

    private TreeStore<ChannelTreeModel> store;

    private TreePanel<ChannelTreeModel> channelTree;

    private MenuItem addChannel;

    private MenuItem editChannel;

    private MenuItem deleteChannel;

    private MenuItem previewChannel;
    
    private ToolButton refreshButton;

    @Inject
    public ChannelsView(GlobalConstants constants, GlobalMessages messages, GlobalResources resources) {

        this.constants = constants;
        this.messages = messages;
        this.resources = resources;

        init();

    }

    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);

    }

    protected void init() {

        store = new TreeStore<ChannelTreeModel>();

        channelTree = new TreePanel<ChannelTreeModel>(store);
        channelTree.setAutoSelect(true);
        channelTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        channelTree.getStyle().setLeafIcon(IconHelper.createStyle("tree-folder"));
        channelTree.setIconProvider(new ModelIconProvider<ChannelTreeModel>() {

            @Override
            public AbstractImagePrototype getIcon(ChannelTreeModel model) {

                switch (model.getType()) {
                case ChannelModel.TYPE_DRAFTS:
                    return AbstractImagePrototype.create(resources.draftsIcon());
                case ChannelModel.TYPE_TRASH:
                    return AbstractImagePrototype.create(resources.trashIcon());
                case ChannelModel.TYPE_SITE:
                    return AbstractImagePrototype.create(resources.siteIcon());
                default:
                    return null;
                }
            }
        });
        channelTree.setDisplayProperty("title");
        Menu contextMenu = new Menu();

        addChannel = new MenuItem(messages.addChannel(), AbstractImagePrototype.create(resources.addIcon()));
        contextMenu.add(addChannel);

        editChannel = new MenuItem(messages.editChannel(), AbstractImagePrototype.create(resources.editIcon()));
        editChannel.setEnabled(false);
        contextMenu.add(editChannel);

        deleteChannel = new MenuItem(messages.deleteChannel(), AbstractImagePrototype.create(resources.deleteIcon()));
        deleteChannel.setEnabled(false);
        contextMenu.add(deleteChannel);

        previewChannel = new MenuItem(messages.preview(), AbstractImagePrototype.create(resources.previewIcon()));
        previewChannel.setEnabled(false);
        contextMenu.add(previewChannel);
        
        channelTree.setContextMenu(contextMenu);

        add(channelTree);
        setHeading(messages.channels());
        setBorders(false);
        setLayout(new FitLayout());
        refreshButton = new ToolButton("x-tool-refresh");
        getHeader().addTool(refreshButton);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void bindTreeItems(List<ChannelTreeModel> roots) {

        ChannelTreeModel drafts = new ChannelTreeModel();
        drafts.setTitle(messages.drafts());
        drafts.setType(ChannelModel.TYPE_DRAFTS);
        drafts.setRank(-100);

        ChannelTreeModel trash = new ChannelTreeModel();
        trash.setTitle(messages.trash());
        trash.setType(ChannelModel.TYPE_TRASH);
        trash.setRank(-200);

        roots.add(drafts);
        roots.add(trash);

        store.removeAll();
        store.add(roots, true);

        for (ChannelTreeModel child : store.getRootItems()) {
            channelTree.setExpanded(child, true);
        }

        //channelTree.expandAll();
        
        if (store.getChildCount() > 0) {
            channelTree.getSelectionModel().select(store.getChild(0), false);
        }
    }

    @Override
    public TreePanel<ChannelTreeModel> getChannelTree() {
        return channelTree;
    }

    @Override
    public MenuItem getAddChannel() {
        return addChannel;
    }

    @Override
    public MenuItem getDeleteChannel() {
        return deleteChannel;
    }

    @Override
    public MenuItem getEditChannel() {
        return editChannel;
    }
    
    @Override
    public MenuItem getPreviewChannel() {
        return previewChannel;
    }

    @Override
    public ToolButton getRefreshButton() {
        return refreshButton;
    }

}
